package lanou.giftsay_01.main.guidemain;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import lanou.giftsay_01.R;
import lanou.giftsay_01.adapter.SearchRvAdapter;
import lanou.giftsay_01.adapter.SearchVpAdapter;
import lanou.giftsay_01.bean.SearchBean;
import lanou.giftsay_01.fragment.body.BoyfriendFragment;
import lanou.giftsay_01.fragment.body.SearchDanPinFragment;
import lanou.giftsay_01.fragment.body.SearchGonglueFragment;
import lanou.giftsay_01.main.categorymain.SelectGiftActivity;
import lanou.giftsay_01.tools.BaseActivity;
import lanou.giftsay_01.tools.NetTool;
import lanou.giftsay_01.tools.URLValues;

/**
 * Created by dllo on 16/8/23.
 */
public class SearchMainActivity  extends BaseActivity implements View.OnClickListener {

    private RecyclerView rv;
    private SearchRvAdapter searchRvAdapter;
    private ImageView imageView;
    private LinearLayout ll;
    private RelativeLayout sreachTwo;
    private RelativeLayout sreachOne;
    private ViewPager viewPager;
    private TabLayout tb;
    private EditText et1;
    private EditText et2;
    private ImageView iv2;
    private TextView textView , textView2;
    private ImageView paiXuIv;
    private PopupWindow pxPopupWindow;

    @Override
    protected int setLayout() {
        return R.layout.activity_sreach;
    }

    @Override
    protected void initView() {
        rv = (RecyclerView) findViewById(R.id.rv_search);
        textView = (TextView) findViewById(R.id.search_tv);
        textView2 = (TextView) findViewById(R.id.search22_tv);
        imageView = (ImageView) findViewById(R.id.search_back);
        iv2 = (ImageView) findViewById(R.id.search2_back);
        paiXuIv = (ImageView) findViewById(R.id.search2_iv);
        paiXuIv.setOnClickListener(this);
        ll = (LinearLayout) findViewById(R.id.ll_search_jump);
        sreachTwo = (RelativeLayout) findViewById(R.id.rl_search2);
        sreachOne = (RelativeLayout) findViewById(R.id.rl_search);
        viewPager = (ViewPager) findViewById(R.id.vp_search2);
        tb = (TabLayout) findViewById(R.id.tb_search2);
        et1 = (EditText) findViewById(R.id.search_et1);
        et2 = (EditText) findViewById(R.id.sreach_et2);

    }

    @Override
    protected void initDate() {
        /**
         * 显示的布局
         */
        GridLayoutManager manager = new GridLayoutManager(this , 4);
        rv.setLayoutManager(manager);
        searchRvAdapter = new SearchRvAdapter(this);
        final NetTool netTool = new NetTool();
        netTool.getNetData(URLValues.SEARCH, SearchBean.class, new NetTool.NetListener<SearchBean>() {
            @Override
            public void onSuccess(SearchBean searchBean) {
                searchRvAdapter.setSearchBean(searchBean);
                rv.setAdapter(searchRvAdapter);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = exchange(et1.getText().toString());
                if (s.equals("")){
                    Toast.makeText(SearchMainActivity.this, "请输入关键字进行搜索", Toast.LENGTH_SHORT).show();
                }else {
                    et2.setText(s);
                    EventBus.getDefault().post(s);
                    sreachOne.setVisibility(View.GONE);
                    sreachTwo.setVisibility(View.VISIBLE);
                }


            }
        });
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SearchMainActivity.this , SelectGiftActivity.class);
                startActivity(intent);
            }
        });
        searchRvAdapter.setSearchOnItemClickListener(new SearchOnItemClickListener() {
            @Override
            public void onClick(int position, SearchBean searchBean) {
                et2.setText(searchBean.getData().getHot_words().get(position));
                EventBus.getDefault().post(searchBean.getData().getHot_words().get(position));
                sreachOne.setVisibility(View.GONE);
                sreachTwo.setVisibility(View.VISIBLE);

            }
        });
        /**
         * 隐藏的布局
         */
          SearchVpAdapter searchVpAdapter = new SearchVpAdapter(getSupportFragmentManager());
          ArrayList<Fragment> fragments = new ArrayList<>();
          fragments.add(new SearchDanPinFragment());
          fragments.add(new SearchGonglueFragment());
          searchVpAdapter.setFragments(fragments);
          ArrayList<String> name = new ArrayList<>();
          name.add("单品");
          name.add("攻略");
          searchVpAdapter.setName(name);
          viewPager.setAdapter(searchVpAdapter);
          tb.setupWithViewPager(viewPager);
        /**
         * 设置监听
         */
          et2.addTextChangedListener(new TextWatcher() {
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {

              }

              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
                  Log.d("SearchMainActivity", "s:" + s);
                  if (start == 0){
                      /**
                       * 下次再显示布局2时显示的是排序的图
                       */
                      paiXuIv.setVisibility(View.VISIBLE);
                      textView2.setVisibility(View.GONE);
                      /**
                       * 布局2隐藏
                       * 布局1显示
                       */
                      sreachTwo.setVisibility(View.GONE);
                      sreachOne.setVisibility(View.VISIBLE);

                  }
              }

              @Override
              public void afterTextChanged(Editable s) {

              }
          });
        /**
         * 监听editText是否获取焦点
         */
         et2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
             @Override
             public void onFocusChange(View v, boolean hasFocus) {
                 if(hasFocus=true){
                     paiXuIv.setVisibility(View.GONE);
                     textView2.setVisibility(View.VISIBLE);
                     /**
                      * 点击搜索
                      */
                     textView2.setOnClickListener(new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {
                             String t = et2.getText().toString();
                             /**
                              * 传值给单品的fragment
                              *
                              */
                             EventBus.getDefault().post(t);
                         }
                     });
                 }
             }
         });

    }
    private String exchange(String context){
        context.replace(" ","");

        return context;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search2_iv:
                if (pxPopupWindow != null && pxPopupWindow.isShowing()){
                    pxPopupWindow.dismiss();
                }else {
                    initPopupWindow();
                    pxPopupWindow.showAsDropDown(v, 0 , 5);
                }
                break;
            case R.id.px2_tv1:
                EventBus.getDefault().post(0);
                pxPopupWindow.dismiss();

                break;
            case R.id.px2_tv2:
                EventBus.getDefault().post(1);
                pxPopupWindow.dismiss();

                break;
            case R.id.px2_tv3:
                EventBus.getDefault().post(2);
                pxPopupWindow.dismiss();

                break;
            case R.id.px2_tv4:
                EventBus.getDefault().post(3);
                pxPopupWindow.dismiss();
                break;
        }

    }
    public void initPopupWindow(){
        View customView = LayoutInflater.from(this).inflate(R.layout.item_popupwindow , null);
        TextView pxOne = (TextView) customView.findViewById(R.id.px2_tv1);
        TextView pxTwo = (TextView) customView.findViewById(R.id.px2_tv2);
        TextView pxThree = (TextView) customView.findViewById(R.id.px2_tv3);
        TextView pxFour = (TextView) customView.findViewById(R.id.px2_tv4);
        pxPopupWindow = new PopupWindow(customView , ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        pxOne.setOnClickListener(this);
        pxTwo.setOnClickListener(this);
        pxThree.setOnClickListener(this);
        pxFour.setOnClickListener(this);
        customView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (pxPopupWindow!= null && pxPopupWindow.isShowing()){
                    pxPopupWindow.dismiss();
                    pxPopupWindow = null ;
                }
                return false;
            }
        });
    }
}
